﻿using RbacMan.Domain.DTO;
using RbacMan.Domain.Entity;
using RbacMan.Repository;

namespace RbacMan.Service
{
    public class DataService
    {
        BaseRepository<Users> UsersbaseRepository;
        BaseRepository<Role> RolebaseRepository;
        BaseRepository<Meun> MeunbaseRepository;
        BaseRepository<RoleUser> RoleUserbaseRepository;
        BaseRepository<RoleMeun> RoleMeunbaseRepository;


        public DataService(BaseRepository<Users> UsersbaseRepository,        BaseRepository<Role> RolebaseRepository,
        BaseRepository<Meun> MeunbaseRepository,        BaseRepository<RoleUser> RoleUserbaseRepository,        BaseRepository<RoleMeun> RoleMeunbaseRepository)
        {
            this.UsersbaseRepository = UsersbaseRepository;
            this.RolebaseRepository = RolebaseRepository;
            this.MeunbaseRepository = MeunbaseRepository;
            this.RoleUserbaseRepository = RoleUserbaseRepository;
            this.RoleMeunbaseRepository = RoleMeunbaseRepository;
        }



    


        #region y用户模块
        //用户登录
        public object LoginUser(string name ,string pwd)
        {
            var list = (
                from user in UsersbaseRepository.GetAll()
                join roleuser in RoleUserbaseRepository.GetAll()
                on user.UsersId equals roleuser.UsersId
                where user.LoginName== name && user.Pwd== pwd
                select new
                {
                    user.UsersId,
                    user.LoginName,
                    user.UserName,
                    user.UserSex,
                    user.UserPhone,
                    user.State,
                    user.AddTime,
                    roleuser.RoleId,

                }).FirstOrDefault();



            return list;

        }


        //用户信息显示
        public List<Users> GetUsers()
        {
            var list = UsersbaseRepository.GetAll().ToList();
            return list;
        }

        #endregion



        #region 角色管理

        //角色信息显示-查询-分页
        public (int totalcount ,int pagecount, List<Role> list) GetRoles(string? name,int pageindex ,int pagesize)
        {
            var list = RolebaseRepository.GetAll().ToList();


            if (!string.IsNullOrEmpty(name))
            {
                list = list.Where(c => c.RoleName.Contains(name)).ToList();
            }
            int totalcount = list.Count();

            int pagecount = (int)Math.Ceiling(totalcount * 1.0 / pagesize);

            list = list.Skip((pageindex - 1) * pagesize).Take(pagesize).ToList();


            return (totalcount, pagecount,list);
        }

        //不同角色的用户信息反填
        public List<RoleUser> GetRoleUser(int roleid)
        {
            var list = RoleUserbaseRepository.GetAll().Where(c => c.RoleId == roleid).ToList();
            return list;
        }

        //角色分配用户

        public int RoleUserAdd(RoleUserDTO roleUser)
        {
            //删除原有数据
            var entitys = RoleUserbaseRepository.GetAll().Where(c => c.RoleId == roleUser.RoleId).ToList();
            RoleUserbaseRepository.DeleteRange(entitys);


            //添加角色用户表
            foreach (var item in roleUser.UsersIdList)
            {
                var roleusers = new RoleUser()
                {
                    RoleId = roleUser.RoleId,
                    UsersId = item
                };
                RoleUserbaseRepository.Add(roleusers);
            }


            return 1;//添加成功


        }

      //不同角色的菜单信息反填
      public    List<RoleMeun> GetRoleMeuns(int roleid)
        {
            var list = RoleMeunbaseRepository.GetAll().Where(c=>c.RoleId== roleid).ToList();
            return list;
        }
        
        
        //角色分配菜单

        public int RoleMeunAdd(RoleMeunDTO roleMeunDTO)
        {
            //删除原有数据
            var entitys = RoleMeunbaseRepository.GetAll().Where(c => c.RoleId == roleMeunDTO.RoleId).ToList();
            RoleMeunbaseRepository.DeleteRange(entitys);


            //添加角色用户表
            foreach (var item in roleMeunDTO.MeunIdList)
            {
                var roleusers = new RoleMeun()
                {
                    RoleId = roleMeunDTO.RoleId,
                    MeunId = item
                };
                RoleMeunbaseRepository.Add(roleusers);
            }


            return 1;//添加成功


        }
        //角色修改-反填
        public Role GetRoleByid(int roleId)
        {
            var list = RolebaseRepository.GetAll().FirstOrDefault(c=>c.RoleId== roleId);
            return list;
        }
        //角色修改-修改
        public int RoleUpdate(Role role)
        {
            role.UpdateTime = DateTime.Now;
            var list = RolebaseRepository.Update(role);
            return list;
        }
        #endregion



        #region 菜单管理


        //菜单显示列表显示
        public List<Meun> GetMeuns()
        {
            var list = MeunbaseRepository.GetAll().Where(c=>!c.IsDel).ToList();
            return list;
        }

        //菜单显示级联显示

        public List<MeunCaseader> GetMeunCaseader(int pid)
        {
            List<MeunCaseader> list = new List<MeunCaseader>();

            var list1 = MeunbaseRepository.GetAll().Where(c => !c.IsDel && c.PID==pid).ToList();

            foreach (var item in list1)
            {
                MeunCaseader caseader = new MeunCaseader();

                caseader.value = item.MeunId;
                caseader.label = item.MeunName;
                caseader.labelUrl = item.MeunUrl;
                caseader.children = GetMeunCaseader(item.MeunId);
                list.Add(caseader);
            }
            return list;
        }


        //菜单根据不同用户进行显示
        public List<MeunCaseader> GetRoleMeunByid(int roleId, int pid = 0)
        {
            List<MeunCaseader> list = new List<MeunCaseader>();
            var list1 = (from rolemeun in RoleMeunbaseRepository.GetAll()
                        join meun in MeunbaseRepository.GetAll()
                        on rolemeun.MeunId equals meun.MeunId
                        where rolemeun.RoleId == roleId && meun.PID== pid
                         select new
                        {
                            meun.MeunId,
                            meun.MeunName,
                            meun.MeunUrl ,
                            meun.PID
                        }).ToList();

            foreach (var item in list1)
            {
                MeunCaseader caseader = new MeunCaseader();

                caseader.value = item.MeunId;
                caseader.label = item.MeunName;
                caseader.labelUrl = item.MeunUrl;
                
                caseader.children = GetRoleMeunByid(roleId,item.MeunId );
                list.Add(caseader);
            }
            return list;


        }



        //菜单添加
        public int MeunAdd(Meun meun)
        {
            meun.AddTime = DateTime.Now;
            var list = MeunbaseRepository.Add(meun);
            return list;
        }

        //菜单的逻辑删除
        public int MeunDel(int meunId)
        {
             
            var list = MeunbaseRepository.GetAll().FirstOrDefault(c => c.MeunId == meunId);
            var entity = MeunbaseRepository.GetAll().Where(c => c.PID == list.MeunId).ToList();
            if (entity.Count > 0)
            {
                return -1;
            }
            list.IsDel = true;
            MeunbaseRepository.Update(list);
            return 1;

        }


        //菜单反填

        public Meun GetMeunById(int meunId)
        {
            var list = MeunbaseRepository.GetAll().FirstOrDefault(c => c.MeunId == meunId);
            return list;
        }


        //菜单修改

        public int MeunUpdate(Meun meun)
        {
            meun.UpdateTime = DateTime.Now;
            var list = MeunbaseRepository.Update(meun);
            return list;
        }








        #endregion

    }
}
